﻿using Anno.EngineData;
using Anno.Loader;
using Anno.Plugs.HelloWorldService;
using Anno.Rpc.Server;
using Microsoft.AspNetCore.Builder;

namespace WebApiAnnoService
{
    public class Startup
    {
        private IServiceCollection services;
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection _services)
        {
            _services.AddControllers();
            IocLoader.DiServices = _services;
            services = _services;
            services.AddSingleton(new AnnoWebApiService());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{Controller=Home}/{action=Index}/{id?}");
            });
            #region 启动Service
            Task.Run(() => {
                Bootstrap.StartUp(new string[] { }, () =>//服务配置文件读取完成后回调(服务未启动)
                {
                    services.AddSingleton(typeof(IRpcConnector), typeof(RpcConnectorImpl));
                }
               , Bootstrap.ApiDoc, IocType.DependencyInjection);
            });
            #endregion
        }
    }
}
